package com.uzai.console.service.app.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.app.customlink.AppCustomLinkDto;
import com.uzai.console.dto.app.customlink.AppCustomLinkQuery;
import com.uzai.console.dto.cms.customlink.CmsCustomLinkQuery;
import com.uzai.console.entity.AppCustomLink;
import com.uzai.console.entity.CmsCustomLink;
import com.uzai.console.mapper.AppCustomLinkMapper;
import com.uzai.console.mapper.CmsCustomLinkMapper;
import com.uzai.console.service.app.AppCustomLinkService;
import com.uzai.console.service.cms.CmsCustomLinkService;
import com.uzai.console.vo.app.customLink.AppCustomLinkVo;
import com.uzai.console.vo.cms.customlink.CmsCustomLinkVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AppCustomLinkServiceImpl implements AppCustomLinkService {

    @Autowired
    private AppCustomLinkMapper appCustomLinkMapper;

    @Autowired
    private CmsCustomLinkService cmsCustomLinkService;

    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    @Autowired
    private CmsCustomLinkMapper cmsCustomLinkMapper;
    @Override
    public Object queryAppCustomLinkList(AppCustomLinkQuery appCustomLinkQuery) {

        List<AppCustomLinkVo> appCustomLinkVoList=new ArrayList<>();

        Page<AppCustomLink>page=new Page<>();
        appCustomLinkQuery.convert(page);

        List<AppCustomLink> appCustomLinkList= appCustomLinkMapper.findByList(page,appCustomLinkQuery);

        if(appCustomLinkList!=null){
            for (AppCustomLink appCustomLink:appCustomLinkList) {
                AppCustomLinkVo appCustomLinkVo=new AppCustomLinkVo();
                BeanUtils.copyProperties(appCustomLink,appCustomLinkVo);
                appCustomLinkVoList.add(appCustomLinkVo);
            }
        }

         Page<AppCustomLinkVo> pageVo=new Page<>();
        BeanUtils.copyProperties(page,pageVo);
        pageVo.setRecords(appCustomLinkVoList);

        return pageVo;
    }

    @Override
    public void addAppCustomLinkList(AppCustomLinkDto appCustomLinkDto) {

        if(StringUtils.isBlank(appCustomLinkDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入名称");
        }

        if(StringUtils.isBlank(appCustomLinkDto.getImg())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入图片");
        }

        if(StringUtils.isBlank(appCustomLinkDto.getUrl())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入链接");
        }

        AppCustomLink appCustomLink = new AppCustomLink();
        Integer now = DateUtil.getNowTime();
        appCustomLink.setId(IdWorker.getId());
        appCustomLink.setCreateTime(now);
        appCustomLink.setUpdateTime(now);
        appCustomLink.setMerId(appCustomLinkDto.getMerId());
        appCustomLink.setLocation(appCustomLinkDto.getLocation());
        appCustomLink.setName(appCustomLinkDto.getName());
        appCustomLink.setImg(appCustomLinkDto.getImg());
        appCustomLink.setType(Tools.getInteger(appCustomLinkDto.getType()));
        appCustomLink.setUrl(appCustomLinkDto.getUrl());
        //保存自定义链接
        appCustomLinkMapper.insertSelective(appCustomLink);
    }

    @Override
    public AppCustomLinkVo queryAppCustomLink(AppCustomLinkDto appCustomLinkDto) {
        if(appCustomLinkDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        AppCustomLink appCustomLink = appCustomLinkMapper.selectById(appCustomLinkDto.getId(), appCustomLinkDto.getMerId());
        if(appCustomLink == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该自定义链接不存在");
        }

        AppCustomLinkVo appCustomLinkVo = new AppCustomLinkVo();
        BeanUtils.copyProperties(appCustomLink, appCustomLinkVo);
        return appCustomLinkVo;
    }

    @Override
    public void updateAppCustomLinkList(AppCustomLinkDto appCustomLinkDto) {
        if(appCustomLinkDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        if(StringUtils.isBlank(appCustomLinkDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入名称");
        }

        if(StringUtils.isBlank(appCustomLinkDto.getImg())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入图片");
        }

        if(StringUtils.isBlank(appCustomLinkDto.getUrl())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入链接");
        }

        AppCustomLink appCustomLink = appCustomLinkMapper.selectById(appCustomLinkDto.getId(), appCustomLinkDto.getMerId());

        if(appCustomLink == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该自定义链接不存在");
        }

        Integer now = DateUtil.getNowTime();
        appCustomLink.setUpdateTime(now);
        appCustomLink.setName(appCustomLinkDto.getName());
        appCustomLink.setImg(appCustomLinkDto.getImg());
        appCustomLink.setUrl(appCustomLinkDto.getUrl());
        appCustomLink.setType(Tools.getInteger(appCustomLinkDto.getType()));

        //保存自定义链接
        appCustomLinkMapper.updateByIdSelective(appCustomLink);
    }


    @Override
    public void deleAppCustomLink(AppCustomLinkDto appCustomLinkDto) {
        if(appCustomLinkDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        AppCustomLink appCustomLink = appCustomLinkMapper.selectById(appCustomLinkDto.getId(), appCustomLinkDto.getMerId());
        if(appCustomLink == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该自自定义链接不存在");
        }

        //删除自定义链接
        appCustomLinkMapper.deleteById(appCustomLinkDto.getId(), appCustomLinkDto.getMerId());
    }


    @Override
    public Object copyCmsCustomLink(Long merId) {
        CmsCustomLinkQuery cmsCustomLinkQuery=new CmsCustomLinkQuery();
        cmsCustomLinkQuery.setMerId(merId);
        cmsCustomLinkQuery.setCurrent(1);
        cmsCustomLinkQuery.setSize(999);
        cmsCustomLinkQuery.setLocation("PersonalGrid");
        Page<CmsCustomLinkVo> cmsCustomLinkList = cmsCustomLinkService.findCmsCustomLinkList(cmsCustomLinkQuery);
        long total = cmsCustomLinkList.getTotal();


        if(cmsCustomLinkList==null||cmsCustomLinkList.getRecords()==null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "无CMS自定义链接列表");
        }

        List<CmsCustomLinkVo> records = cmsCustomLinkList.getRecords();
        List<AppCustomLinkVo> appCustomLinkVoList=new ArrayList<>();
        for (CmsCustomLinkVo cmsCustomLinkVo:records) {
            AppCustomLinkVo appCustomLinkVo = new AppCustomLinkVo();
            if(cmsCustomLinkVo.getName()==null||cmsCustomLinkVo.getName().equals("转链")||cmsCustomLinkVo.getName().equals("搜索")){
                total--;
                continue;
            }
            BeanUtils.copyProperties(cmsCustomLinkVo,appCustomLinkVo);
            appCustomLinkVoList.add(appCustomLinkVo);
        }
        Page<AppCustomLinkVo> page=new Page<>();
       page.setCurrent(cmsCustomLinkList.getCurrent());
       page.setSize(cmsCustomLinkList.getSize());
       page.setTotal(total);
       page.setPages(cmsCustomLinkList.getPages());
       page.setOrders(cmsCustomLinkList.getOrders());
       page.setRecords(appCustomLinkVoList);
        return page;
    }


    @Override
    public Object copyDefaultAppCustomLink(Long merId) {
        List<AppCustomLinkVo> appCustomLinkVoList=new ArrayList<>();

        Long defaultMerId = uzaiConsoleSysConfig.getDefaultMerId();
        //查询默认账号的自定义配置
//        CmsCustomLinkQuery cmsCustomLinkQuery = new CmsCustomLinkQuery();
//        cmsCustomLinkQuery.setMerId(defaultMerId);
//        List<CmsCustomLink> cmsCustomLinkList = cmsCustomLinkMapper.findByList(cmsCustomLinkQuery);

//        if(cmsCustomLinkList!=null&&cmsCustomLinkList.size()>0){
//
//            for (CmsCustomLink cmsCustomLink:cmsCustomLinkList) {
//
//                if(cmsCustomLink.getName()==null||cmsCustomLink.getName().equals("转链")||cmsCustomLink.getName().equals("搜索")){
//                    continue;
//                }
//
//                AppCustomLink appCustomLink1 = new AppCustomLink();
//                BeanUtils.copyProperties(cmsCustomLink,appCustomLink1);
//                AppCustomLink appCustomLink= appCustomLinkMapper.selectByName(appCustomLink1.getName(),merId);
//                if(appCustomLink==null){
//                    appCustomLink1.setId(IdWorker.getId());
//                    appCustomLink1.setMerId(merId);
//                    appCustomLinkMapper.insertSelective(appCustomLink1);
//                    AppCustomLinkVo appCustomLinkVo = new AppCustomLinkVo();
//                    BeanUtils.copyProperties(appCustomLink1,appCustomLinkVo);
//                    appCustomLinkVoList.add(appCustomLinkVo);
//                }
//            }
//        }

        //查询默认账号的app自定义配置
        AppCustomLinkQuery appCustomLinkQuery=new AppCustomLinkQuery();
        appCustomLinkQuery.setMerId(defaultMerId);
        List<AppCustomLink> appCustomLinkList= appCustomLinkMapper.findByList(appCustomLinkQuery);

        if(appCustomLinkList!=null&&appCustomLinkList.size()>0){

            //删除以前的配置
            List<AppCustomLink> appCustomLinkList1=appCustomLinkMapper.selectByMer(merId);
            appCustomLinkList1.forEach(it->{
                appCustomLinkMapper.deleteById(it.getId(),merId);
            });

            //插入默认配置
            for (AppCustomLink appCustomLink:appCustomLinkList) {
                if(appCustomLink.getName()==null||appCustomLink.getName().equals("转链")||appCustomLink.getName().equals("搜索")){
                    continue;
                }
                    appCustomLink.setId(IdWorker.getId());
                    appCustomLink.setMerId(merId);
                    appCustomLinkMapper.insertSelective(appCustomLink);
                    AppCustomLinkVo appCustomLinkVo = new AppCustomLinkVo();
                    BeanUtils.copyProperties(appCustomLink,appCustomLinkVo);
                    appCustomLinkVoList.add(appCustomLinkVo);

            }
        }
        return appCustomLinkVoList;
    }
}
